import Cookies from 'js-cookie';
import useUserStore from '@/store/modules/userinfo';

const TokenKey = 'access_token';

export function getToken() {
	return Cookies.get(TokenKey);
}

export function setToken(token: string) {
	return Cookies.set(TokenKey, token);
}

export function removeToken() {
	return Cookies.remove(TokenKey);
}

/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkPermi(value: string[]) {
	if (value && value instanceof Array && value.length > 0) {
		const permissions = useUserStore().permissions;
		const permissionDatas = value;
		const all_permission = '*:*:*';

		// const hasPermission = permissions.some((permission: string) => {
		// 	return all_permission === permission || permissionDatas.includes(permission);
		// });

		// if (!hasPermission) {
		// 	return false;
		// }
		return true;
	} else {
		console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`);
		return false;
	}
}
